//
//  TCBaseTool.m
//  陶瓷
//
//  Created by 郭天龙 on 15/7/26.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "TCBaseTool.h"
#import "TCNetworkTool.h"

@interface TCBaseTool()<BMKLocationServiceDelegate>

@property (nonatomic, strong)BMKLocationService *locService;

@end


@implementation TCBaseTool

+ (TCBaseTool *)sharedBaseTool {
    static TCBaseTool *_sharedBaseTool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedBaseTool = [[self alloc] init];
    });
    return _sharedBaseTool;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _locService = [[BMKLocationService alloc] init];
        _locService.delegate = self;
        _currentCityId = [[NSMutableString alloc] initWithCapacity:20];
        _currentCityName = [[NSMutableString alloc] initWithCapacity:20];
        [_currentCityId setString:@""];
        [_currentCityName setString:@""];
    }
    return self;
}


//根据日期转换为对应时间
+ (NSString *)calculateTime:(NSString *)time
{
    NSString *standardTempTime = [time stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    NSRange range;
    range.length = standardTempTime.length - [NSString stringWithFormat:@"2015-07-26 11:10:58"].length;
    range.location = [NSString stringWithFormat:@"2015-07-26 11:10:58"].length;
    NSString *standardTime = [standardTempTime stringByReplacingCharactersInRange:range withString:@""];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:standardTime];
    CGFloat times = (long)[date timeIntervalSince1970];
    
    int distance = [[NSDate date] timeIntervalSince1970] - times;
    NSString *string;
    if (distance < 1){//avoid 0 seconds
        string = @"刚刚";
    }
    else if (distance < 60) {
        string = [NSString stringWithFormat:@"%d秒前", (distance)];
    }
    else if (distance < 3600) {//60 * 60
        distance = distance / 60;
        string = [NSString stringWithFormat:@"%d分钟前", (distance)];
    }
    else if (distance < 86400) {//60 * 60 * 24
        distance = distance / 3600;
        string = [NSString stringWithFormat:@"%d小时前", (distance)];
    }
    else if (distance < 604800) {//60 * 60 * 24 * 7
        distance = distance / 86400;
        string = [NSString stringWithFormat:@"%d天前", (distance)];
    }
    else if (distance < 2419200) {//60 * 60 * 24 * 7 * 4
        distance = distance / 604800;
        string = [NSString stringWithFormat:@"%d周前", (distance)];
    }
    else {
        NSDateFormatter *dateFormatter = nil;
        if (dateFormatter == nil) {
            dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        }
        string = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(times)]];
        
    }
    return string;
}

#pragma mark - BMKLocationServiceDelegate
/**
 *  初始化百度地图用户位置管理类
 */
- (void)initBMKUserLocation
{
    [_locService startUserLocationService];
    
}

/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation;
{
    self.locaLatitude = userLocation.location.coordinate.latitude;
    self.locaLongitude = userLocation.location.coordinate.longitude;
    [_locService stopUserLocationService];
    
    NSString *location = [NSString stringWithFormat:@"%f,%f",self.locaLatitude,self.locaLongitude];
    [[TCNetworkTool sharedNetTool] getCityIDWithLocation:location andSuccessBlocks:^(NSDictionary *successMessage) {
        [self.currentCityId setString:[NSString stringWithFormat:@"%@",[successMessage objectForKey:@"Id"]]];
        [self.currentCityName setString:[successMessage objectForKey:@"Name"]];
//        self.currentCityId = [NSString stringWithFormat:@"%@",[successMessage objectForKey:@"Id"]];
//        self.currentCityName = [successMessage objectForKey:@"Name"];
    } andFailureBlocks:^(NSString *failureMessage) {
        [MBHUDHelper showWarningWithText:failureMessage];
    }];
}

- (void)NetCityIdFromPositionlatitiyde:(CGFloat)latit longitude:(CGFloat)longitude SuccessBlock:(NetCityIdSuccessBlock)success
{
    NSString *location = [NSString stringWithFormat:@"%f,%f",latit,longitude];
    [[TCNetworkTool sharedNetTool] getCityIDWithLocation:location andSuccessBlocks:^(NSDictionary *successMessage) {
        [self.currentCityId setString:[NSString stringWithFormat:@"%@",[successMessage objectForKey:@"Id"]]];
        [self.currentCityName setString:[successMessage objectForKey:@"Name"]];
        success();
    } andFailureBlocks:^(NSString *failureMessage) {
        [MBHUDHelper showWarningWithText:failureMessage];
    }];
}



@end
